package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.ImageBiz;
import cn.sumpu.app.guoguojie.constant.DoingType;
import cn.sumpu.app.guoguojie.constant.FeedIdType;
import cn.sumpu.app.guoguojie.constant.FeedType;
import cn.sumpu.app.guoguojie.dao.DoingDao;
import cn.sumpu.app.guoguojie.dao.FeedDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.Feed;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class DoingBizImpl implements DoingBiz {
	private DoingDao doingDao;
	private FeedDao feedDao;
	@Autowired
	private ImageBiz imageBiz;

	public void setDoingDao(DoingDao doingDao) {
		this.doingDao = doingDao;
	}
	
	//输入一条微博，同时插入一个动态
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void insert(Doing doing) throws AppBusinessException {
		doing.setIdentifier(IdGenerator.createIdentifier());
		doingDao.insert(doing);
		// 添加动态
		Feed feed = new Feed();
		feed.setIdentifier(IdGenerator.createIdentifier());
		feed.setMember(doing.getMember());
		feed.setFeedType("doing");
		feed.setFeedIdType("doingid");
		feed.setBodyData(doing.getContent());
		feed.setFeedId(doing.getIdentifier());
		feed.setFamily(doing.getFamily());
		feed.setImg1Path(doing.getPictureThumbnail());
		feed.setImg2Path(doing.getPictureUrl());
		feed.setImageServer(doing.getImageServer());
		feedDao.insert(feed);
	}
	
	//转发微博
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void doingForward(String forwardId,String content,Member member) throws AppBusinessException {
		Doing forwardDoing = doingDao.getDoingById(forwardId);
		doingDao.updateForwards(forwardDoing.getRootId());
		doingDao.updateForwards(forwardId);
		Doing doing = new Doing();
		doing.setIdentifier(IdGenerator.createIdentifier());
		doing.setMember(member.getIdentifier());
		doing.setFamily(member.getFamily());
		doing.setContent(content);
		doing.setType(DoingType.FORWARD.getValue());
		doing.setRootId(Common.empty(forwardDoing.getRootId())?forwardId:forwardDoing.getRootId());
		doing.setToId(forwardId);
		doingDao.insert(doing);
		// 添加动态
		Feed feed = new Feed();
		feed.setIdentifier(IdGenerator.createIdentifier());
		feed.setMember(member.getIdentifier());
		feed.setFeedType("doing");
		feed.setFeedIdType("doingid");
		feed.setBodyData(doing.getContent());
		feed.setFeedId(doing.getIdentifier());
		feed.setFamily(member.getFamily());
		feedDao.insert(feed);
	}

	public void setFeedDao(FeedDao feedDao) {
		this.feedDao = feedDao;
	}

	public void update(Doing doing) throws AppBusinessException {
		doingDao.update(doing);
	}
	public Doing getDoingByMember(String member) throws AppBusinessException {
		return doingDao.getDoingByMember(member);
	}
	public Doing getDoingByFamily(String family) throws AppBusinessException {
		return doingDao.getDoingByFamily(family);
	}
	public Doing getDoingById(String id) throws AppBusinessException {
		return doingDao.getDoingById(id);
	}

	
	@Override
	public Doing getRootDoingById(String id) throws AppBusinessException {
		Doing doing = doingDao.getDoingById(id);
		if(DoingType.FORWARD.getValue().equals(doing.getType())){
			Doing rootDoing = doingDao.getDoingById(doing.getRootId());
			if(rootDoing == null){
				return rootDoing = new Doing();
			}
			rootDoing.setBaseType(DoingType.FORWARD.getValue());
			return rootDoing;
		}
		return null;
	}

	public List<Doing> getDoingList(Map<String, Object> map)
			throws AppBusinessException {
		return doingDao.getDoingList(map);
	}
	@Override
	public int getDoingListCount(String family)throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		return doingDao.getDoingListCount(map);
	}
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void deleteDoing(Member member,String id,String webAppPath) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("identifier", id);
		map.put("family", member.getFamily());
		Doing delDoing = doingDao.getDoingById(id);
		Common.deleteImage(webAppPath,delDoing.getPictureThumbnail());
		Common.deleteImage(webAppPath,delDoing.getPictureUrl());
		doingDao.deleteDoing(map);
		//从服务器上删除图片
		/*imageBiz.deleteImage(delDoing.getPictureThumbnail());
		imageBiz.deleteImage(delDoing.getPictureUrl());*/
		//删除动态表的内容
		map.put("feedType", FeedType.DOING.getValue());
		map.put("feedId", id);
		map.put("feedIdType", FeedIdType.DOINGID.getValue());
		map.put("member", member.getIdentifier());
		feedDao.deleteFeed(map);
	}
	
	

	@Override
	public void deleteDoingImg(Doing doing,String webAppPath) throws AppBusinessException {
		if(doing==null) return ;
		if(Common.empty(doing.getImageServer())){
			Common.deleteImage(webAppPath, doing.getPictureThumbnail());
			Common.deleteImage(webAppPath, doing.getPictureUrl());
		}else{
			imageBiz.deleteImage(doing.getPictureThumbnail());
			imageBiz.deleteImage(doing.getPictureUrl());
		}
	}

	@Override
	public PageModel getDoingModel(String family,Integer page) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		Integer startId = (page - 1) * Common.PAGE_SIZE_DOING;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_DOING);
		List<Doing> doingList = doingDao.getDoingList(map);
		if(doingList!=null && doingList.size()>0){
			for (Doing doing : doingList) {
				String type = doing.getType();
				if(DoingType.FORWARD.getValue().equals(type)){
					Doing rootDoing = getDoingById(doing.getRootId());
					doing.setRootDoing(rootDoing); 
				}
			}
		} 
		
		int count = doingDao.getDoingListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(doingList);
		pm.setPageSize(Common.PAGE_SIZE_DOING);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_DOING - 1) / Common.PAGE_SIZE_DOING);
		return pm;
	}
	
	
}
